import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tender_flutter/constant/app_config.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/page/goods/api/api_goods.dart';
import 'package:tender_flutter/page/person/api/api_person.dart';
import 'package:tender_flutter/page/person/model/my_news_entity.dart';
import 'package:tender_flutter/router/router.dart';
import 'package:tender_flutter/router/router_map.gr.dart';
import 'package:tender_flutter/style/font_style.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/widgets/base_appbar.dart';
import 'package:tender_flutter/widgets/load_state_layout.dart';

import 'item/goods_cell.dart';
import 'model/goods_entity.dart';

class TabGoodsPage extends StatefulWidget {
  @override
  _GoodsPageState createState() => _GoodsPageState();
}

class _GoodsPageState extends State<TabGoodsPage>
    with AutomaticKeepAliveClientMixin {
  RefreshController _refreshController =
  RefreshController(initialRefresh: false);

  Future<ApiResponse<GoodsEntity>> _future;

  LoadState _loadState = LoadState.State_Loading;

  List<GoodsList> _dataLists = [];

  ///刷新数据
  Future _refresh() async {
    setState(() {
      _future = _getData();
    });
  }

  /// 获取数据
  Future<ApiResponse<GoodsEntity>> _getData() async {
    var response = await ApiGoods.getGoodsList<GoodsEntity>(
    );
    return response;
  }

  FutureBuilder<ApiResponse<GoodsEntity>> _buildFutureBuilder() {
    return FutureBuilder(
        future: _future,
        builder: (BuildContext context,
            AsyncSnapshot<ApiResponse<GoodsEntity>> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasData) {
              GoodsEntity entity = snapshot.data.result;
              if (snapshot.data.status == Status.SUCCESS) {
                List temps = entity.xList == null ? [] : entity.xList;
                _dataLists = temps;
                if (_dataLists.isEmpty) {
                  _loadState = LoadState.State_Empty;
                } else {
                  _loadState = LoadState.State_Success;
                }
              } else {
                _loadState = LoadState.State_Error;
              }
            } else if (snapshot.hasError) {
              _loadState = LoadState.State_Error;
            }
            if (_refreshController.isRefresh) {
              _refreshController.refreshCompleted();
            }
          }

          return LoadStateLayout(
            bgColor: ThemeColors.colorWhite,
            state: _loadState,
            successWidget: _initBody(),
          );
        });
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    _future = _getData();
  }

  @override
  Widget build(BuildContext context) {;
    return Scaffold(
      backgroundColor: ThemeColors.colorWhite,
      appBar: _initAppBar(),
      body:_buildFutureBuilder(),
    );
  }

  /// 导航
  Widget _initAppBar() {
    return BaseAppBar(context, Text("商城中心", style: FonStyle.navTextStyle()),isShowLeading: false);
  }


  /// 内容
  Widget _initBody() {
    return SmartRefresher(
        physics: BouncingScrollPhysics(),
        controller: _refreshController,
        enablePullUp: false,
        enablePullDown: true,
        onRefresh: _refresh,
        child: ListView.separated(
          itemCount: _dataLists.length,
          itemBuilder: (BuildContext context, int index) {
            GoodsList model = _dataLists[index];
            return GoodsCell(
                data: model,
                onTap: () {
                  LRouter.navigatorCtx(context).push(
                      Routes.goodsDetailPage, arguments: GoodsDetailPageArguments(goodsId: model.id));
                });
          },
          separatorBuilder: (BuildContext context, int index) {
            return Divider(
              height: ScreenAdapter.height(1),
              color: ThemeColors.colorType11,
            );
          },
        ));
  }
}
